package com.face.server.service.impl;

import com.face.RespMsg;
import com.face.server.dao.DormAdminDao;
import com.face.server.entity.Admin;
import com.face.server.entity.DormitoryBuild;
import com.face.server.repository.DormitoryBuildRepository;
import com.face.server.vo.DormAdmin;
import com.face.server.convert.DormitoryAdmin2DormAdmin;
import com.face.server.entity.DormitoryAdmin;
import com.face.server.repository.DormAdminRepository;
import com.face.server.repository.exRepository.exDormAdminRepository;
import com.face.server.service.DormAdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.Principal;
import java.util.List;

/**
 * Created by ryan on 2018/1/26.
 */
@Service
public class DormAdminServiceImpl implements DormAdminService {
    @Autowired
    private DormAdminRepository dormAdminRepository;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private exDormAdminRepository exDormAdminRepository;

    @Autowired
    private DormAdminDao dormAdminDao;

    @Autowired
    private DormitoryBuildRepository dormitoryBuildRepository;

    @Autowired
    FaceDetectImpl faceDetect;

    @Override
    public Page<DormitoryAdmin> search(Pageable pageRequest, String key1, String key2) {
        if ((key1==null||key1.equals(""))&&(key2==null||key2.equals(""))){
            List<DormitoryAdmin> dormitoryAdminList=dormAdminDao.findAll(pageRequest);
            return new PageImpl<DormitoryAdmin>(dormitoryAdminList,pageRequest,dormitoryAdminList.size());
        }
        List<DormitoryAdmin> dormitoryAdmins=exDormAdminRepository.search(pageRequest,key1,key2);
        return new PageImpl<DormitoryAdmin>(dormitoryAdmins,pageRequest,dormitoryAdmins.size());
    }

    @Override
    public Page<DormitoryAdmin> search(Pageable pageRequest, String key1, String key2, Principal principal) {
        Subject subject = SecurityUtils.getSubject();
        Admin admin = (Admin) subject.getPrincipal();
        List<Integer> dormitoryNumList=admin.getDormitoryNumList();
        if ((key1==null||key1.equals(""))&&(key2==null||key2.equals(""))){
            List<DormitoryAdmin> dormitoryAdminList=dormAdminDao.findByDormitoryBuildNumIn(dormitoryNumList,pageRequest);
            return new PageImpl<DormitoryAdmin>(dormitoryAdminList,pageRequest,dormitoryAdminList.size());
        }
        List<DormitoryAdmin> dormitoryAdmins=exDormAdminRepository.search(pageRequest,key1,key2,dormitoryNumList);
        return new PageImpl<DormitoryAdmin>(dormitoryAdmins,pageRequest,dormitoryAdmins.size());
    }

    @Override
    public String update(DormitoryAdmin dormitoryAdmin) {
        int i=dormAdminDao.update(dormitoryAdmin);
        return i==1?"更新成功":"更新失败";
    }

    @Override
    public String delete(String id) {
        int i=dormAdminDao.delete(id);
        return i==1?"删除成功":"删除失败";
    }

    @Override
    public DormitoryAdmin create(DormitoryAdmin dormitoryAdmin) {
        DormitoryBuild dormitoryBuild=dormitoryBuildRepository.findByBuildingNum(dormitoryAdmin.getDormitoryBuildNum());
        dormitoryAdmin.setType(2);
        dormitoryAdmin.setDormitoryBuild(dormitoryBuild);
        int i=dormAdminDao.create(dormitoryAdmin);
        return null;
    }

    @Override
    public RespMsg updateFace(String id, String fid, MultipartFile file) throws IOException {
        DormitoryAdmin dormitoryAdmin=dormAdminDao.findOne(id);
        return faceDetect.updateFaceModels(dormitoryAdmin,fid,file);
    }

    @Override
    public RespMsg addFace(String id, MultipartFile file) throws IOException {
        DormitoryAdmin dormitoryAdmin=dormAdminDao.findOne(id);
        return faceDetect.saveFaceModels(file,dormitoryAdmin);
    }


}
